package com.bbzn.user.client.service.impl;

import com.bbzn.user.client.dao.system.SystemParamMapper;
import com.bbzn.user.client.dataobject.SystemParam;
import com.bbzn.user.client.dto.system.SystemParamDTO;
import com.bbzn.user.client.service.SystemParamService;
import com.bbzn.user.client.utils.PageInfo;
import com.bbzn.user.client.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
public class SystemParamServiceImpl implements SystemParamService {

    @Resource
    private SystemParamMapper systemParamMapper;

    @Autowired
    private RedisUtils<String> redisUtil;

    @Override
    public int deleteByPrimaryKey(Long id) {
        return systemParamMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(SystemParam record) {
        return systemParamMapper.insert(record);
    }

    @Override
    public int insertSelective(SystemParam record) {
        return systemParamMapper.insertSelective(record);
    }

    @Override
    public SystemParam selectByPrimaryKey(Long id) {
        return systemParamMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(SystemParam record) {
        return systemParamMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(SystemParam record) {
        return systemParamMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<SystemParam> selectSelective(SystemParam record) {
        return systemParamMapper.selectSelective(record);
    }

    @Override
    public PageInfo<SystemParam> selectPageSelective(SystemParamDTO record) {
        record.setStartIndex((record.getPageNum() - 1) * record.getPageSize());
        PageInfo<SystemParam> pageInfo = new PageInfo<>();
        pageInfo.setPageNum(record.getPageNum());
        pageInfo.setPageSize(record.getPageSize());
        pageInfo.setResultList(systemParamMapper.selectPageSelective(record));
        int count = systemParamMapper.selectCountPageSelective(record);
        pageInfo.setTotalRecord(count);
        return pageInfo;
    }

    @Override
    public List<SystemParam> selectBatchIds(List<Long> ids) {
        return systemParamMapper.selectBatchIds(ids);
    }

    @Override
    public int deleteBatch(List<Long> ids) {
        return systemParamMapper.deleteBatch(ids);
    }

    @Override
    public void redisCleanSystemParamJob() {
        List<SystemParam> systemParamList = systemParamMapper.getSystemParamGroupByType();
        if (CollectionUtils.isEmpty(systemParamList)) {
            log.info("没有查询到系统参数数据");
            return;
        }
        systemParamList.stream().forEach(sParam -> {
            boolean hExists = redisUtil.hExists(sParam.getType(), sParam.getKey());
            //存在修改Redis中的数据
            if (hExists) {
                redisUtil.delete(sParam.getType());
            }
        });
    }
}
